﻿namespace GE
{
    using System;
    using System.Threading;
    using System.Windows.Forms;
    using FC.GEPluginCtrls;
    using Microsoft.CSharp.RuntimeBinder;

    public partial class Form2 : Form
    {
        private Nori6UI workerObject;
        private Thread workerThread;

        public Form2()
        {
            InitializeComponent();

            geWebBrowser1.LoadEmbeddedPlugin();

            // Handle the he PluginReady event 
            geWebBrowser1.PluginReady += (o, e) =>
            {
                // Here you can now use 'ge' exactly as you would
                // in the native javascript api. Whenever you make a 
                // call to an api member, you should check for any RuntimeBinderExceptions
                // Register the controls with the browser
                kmlTreeView1.SetBrowserInstance(geWebBrowser1);
                geStatusStrip1.SetBrowserInstance(geWebBrowser1);
                geToolStrip1.SetBrowserInstance(geWebBrowser1);

                FC.GEPluginCtrls.GEHelpers.EnableLayer(e.ApiObject, FC.GEPluginCtrls.Layer.Borders, true);
                FC.GEPluginCtrls.GEHelpers.EnableLayer(e.ApiObject, FC.GEPluginCtrls.Layer.Buildings, true);

                try
                {
                    workerObject = new Nori6UI(geWebBrowser1);
                    workerThread = new Thread(workerObject.DoWork);
                    workerThread.Start();
                }
                catch (System.Exception ex)
                {
                    workerObject = null;
                    System.Environment.Exit(1);
                }
            };

            // handle any script errors
            this.geWebBrowser1.ScriptError += (o, e) =>
            {
                MessageBox.Show(e.Message, e.Data);
            };
        }
    }
}